import os
from win32com import client

"""
@FileName：DocToDocx.py
@Description：
    doc 文件转 docx 文件
@Author：HeYiQing
@Time：2024/5/6 18:30
"""


# param:path 绝对路径 , file_name 文件名称
def doc_to_docx(path, file_name):
    abs_path = os.path.join(path, file_name)
    print("路径：{}".format(abs_path))
    if not os.path.isabs(abs_path):
        print(f"不是绝对路径")
        return
    if not os.path.exists(abs_path):
        print("文件不存在")
        return
    if os.path.splitext(abs_path)[1] != '.doc':
        print("文件类型不对")
        return
    app = client.Dispatch('Word.Application')
    app.DisplayAlerts = False
    document = app.Documents.Open(abs_path)
    document.SaveAs(os.path.splitext(abs_path)[0] + ".docx", 12)
    document.Close()
    app.Quit()
    print("================>Successful<======================== \n\t路径：{} ".format(
        os.path.splitext(abs_path)[0] + ".docx"))


if __name__ == '__main__':
    path_A = r"E:\code_special\Gitee\Math_C\repo\A组--69名测试者"
    path_B = r"E:\code_special\Gitee\Math_C\repo\B组--79名测试者"
    listdir_A = os.listdir(path_A)
    listdir_B = os.listdir(path_B)
    current_dir = os.path.abspath(".")
    for filename in listdir_A:
        doc_to_docx(path_A, filename)
    for filename in listdir_B:
        doc_to_docx(path_B, filename)
